package cn.lyjuan.android.mylibrary.util;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Process;
import android.support.v4.content.PermissionChecker;

import cn.lyjuan.android.mylibrary.context.BaseApplication;

/**
 * 定位权限工具
 * 参考：https://www.cnblogs.com/wlrhnh/p/7389930.html
 * Created by chad on 14/11/2017.
 */
public class BluetoothUtils
{
    private static final String TAG = BluetoothUtils.class.getSimpleName();

    /**
     * 应用是否可定位，包含系统是否开启定位功能及应用是否授予定位权限
     *
     * @return
     */
    public static boolean hasPermission()
    {
        return isLocationEnabled() && isLocationGranted();
    }

    /**
     * 是否授予应用定位权限
     *
     * @return
     */
    public static boolean isLocationGranted()
    {
        Context context = BaseApplication.getContext();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            boolean right = context.checkSelfPermission(Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED;
            if (!right) return false;
            right = context.checkSelfPermission(Manifest.permission.BLUETOOTH_ADMIN) == PackageManager.PERMISSION_GRANTED;
            if (!right) return false;
            right = context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
            if (!right) return false;
            right = context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
            if (!right) return false;

            return true;
        } else
        {
            boolean right = PermissionChecker.checkPermission(context,
                    Manifest.permission.BLUETOOTH, Process.myPid(), Process.myUid(),
                    BaseApplication.getContext().getPackageName()) == PackageManager.PERMISSION_GRANTED;
            if (!right) return false;
            right = PermissionChecker.checkPermission(context,
                    Manifest.permission.BLUETOOTH_ADMIN, Process.myPid(), Process.myUid(),
                    BaseApplication.getContext().getPackageName()) == PackageManager.PERMISSION_GRANTED;
            if (!right) return false;
            right = PermissionChecker.checkPermission(context,
                    Manifest.permission.ACCESS_FINE_LOCATION, Process.myPid(), Process.myUid(),
                    BaseApplication.getContext().getPackageName()) == PackageManager.PERMISSION_GRANTED;
            if (!right) return false;
            right = PermissionChecker.checkPermission(context,
                    Manifest.permission.ACCESS_COARSE_LOCATION, Process.myPid(), Process.myUid(),
                    BaseApplication.getContext().getPackageName()) == PackageManager.PERMISSION_GRANTED;
            if (!right) return false;

            return true;
        }
    }

    /**
     * 系统是否开启定位服务
     *
     * @return
     */
    public static boolean isLocationEnabled()
    {
        Context context = BaseApplication.getContext();

        BluetoothAdapter adp = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
        {
            adp = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
        } else
        {
            adp = BluetoothAdapter.getDefaultAdapter();
        }
        if (null == adp) return false;

        try
        {
            boolean ok = adp.isEnabled();
            if (ok) return true;

            ok = adp.enable();// 尝试打开
            return ok;
        } catch (Throwable t)
        {
            return false;
        }
    }
}
